package com.sunnyatwal.kettlebell.clock;

import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import com.sunnyatwal.kettlebell.R;
import com.sunnyatwal.kettlebell.clock.display.ClockNotification;
import com.sunnyatwal.kettlebell.model.Exercise;
import com.sunnyatwal.kettlebell.model.ElapsedTimeResult;
import com.sunnyatwal.kettlebell.sounds.BeepIDManager;
import com.sunnyatwal.kettlebell.sounds.BeepManager;

public class ClockService extends Service {

	private static final String TAG = "ClockService";
	
	private static final String INTENT_FILTER_CLOCK = "com.sunnyatwal.kettlebell.clock";
	private final static String INTENT_NAME_EXERCISE = "exercise";
	
	private static final int TIMER_INTERVAL = 100;
	
	private Exercise mExercise = null;
	
	private ClockNotification mClockNotif;
	private MyCountDownTimer mCurrentWorkoutTimer;		
	private BeepManager mBeepManager;
		
	private final IBinder mBinder = new LocalBinder();
	
	
	public class LocalBinder extends Binder {
		public ClockService getService() {
            return ClockService.this;
        }
    }
	
	
	@Override
	public IBinder onBind(Intent intent) {
		return mBinder;
	}
	
	
	@Override
	public void onCreate() {
		Log.i(TAG, "onCreate");
		
		BeepIDManager beeps = new BeepIDManager(this, R.raw.beep_count, R.raw.beep_next, R.raw.beep_complete);
		mBeepManager = new BeepManager(beeps);
	}
	
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {		
		Log.i(TAG, "onStartCommand");
		
		if (intent.hasExtra(INTENT_NAME_EXERCISE)) {
			mExercise = intent.getExtras().getParcelable(INTENT_NAME_EXERCISE);
		}
		
		mExercise.insertCountDownAtCurrentIndex();
		
		NotificationManager notifManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
		
		mClockNotif = new ClockNotification(notifManager, this);
					
		mCurrentWorkoutTimer = 
				new MyCountDownTimer(mExercise.getTotalTime(), TIMER_INTERVAL) {
			
					@Override
					public void onTick(int pMillisInFuture, int pCountDownInterval) {
						
						ElapsedTimeResult result = mExercise.ElapseTime(pCountDownInterval);
						
						//Extend the length of the timer to compensate for lost ticks
						if(result.getExtra() > 0) {
							this.extendTimer(result.getExtra());
						}
						
						mBeepManager.checkForBeeps(mExercise);
						
						mClockNotif.ToDisplay(mExercise);
						
						broadcastCurrentTime();
					}
			
					@Override
					public void onFinish() {
						Log.i(TAG, "onFinish");
						
						broadcastCurrentTime();						
						mClockNotif.ToDisplay(mExercise);
						}
					};
						
			mCurrentWorkoutTimer.start();
			
		
		    return Service.START_STICKY;
	}
	
	
	@Override
	public void onDestroy() {
		Log.i(TAG, "onDestroy");
		
		mClockNotif.Reset();
		mCurrentWorkoutTimer.killTimer();

		Log.i(TAG, "Service stopped");
	}
	
	
	private void broadcastCurrentTime() {
		Log.i(TAG, "broadcastCurrentTime");
		
		Intent i = new Intent(INTENT_FILTER_CLOCK);		
		i.putExtra(INTENT_NAME_EXERCISE, mExercise);		
        this.sendBroadcast(i);        
	}
	
	
	
	public void PauseClock() {
		Log.i(TAG, "PauseClock");
		
		mCurrentWorkoutTimer.togglePause();
		
		if(mCurrentWorkoutTimer.isPaused()) {
			mExercise.insertCountDownAtCurrentIndex();	
		}
	}
	
	
	public boolean isFinished() {
		Log.i(TAG, "isFinished");
		
		if(mExercise == null) {
			return false;
		}
		
		return mExercise.isFinished(); 
	}
}
